package cn.changhong.user.service.impl;

import cn.changhong.user.dao.AuthenticationMapper;
import cn.changhong.user.dao.RoleAuthenticationMapper;
import cn.changhong.user.dao.RoleMapper;
import cn.changhong.user.model.*;
import cn.changhong.user.model.vo.RoleVO;
import cn.changhong.user.service.RoleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author zz
 * @Date
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleAuthenticationMapper roleAuthenticationMapper;
    @Autowired
    AuthenticationMapper authenticationMapper;



    @Override
    public List<RoleVO> listRole() {
        List<RoleVO> voList=new ArrayList<>();
        List<Role> list=roleMapper.selectByExample(new RoleExample());
        Iterator<Role> itr=list.iterator();
        while (itr.hasNext()){
            Role role=itr.next();

            voList.add(new RoleVO(role,this.listRoleAuthentication(role.getId())));
        }
        return voList;
    }

    @Override
    public int freezeRole(int roleId) {
        Role role=new Role();

        RoleExample example=new RoleExample();
        example.createCriteria().andIdEqualTo(roleId);
        Role role1=roleMapper.selectByExample(example).get(0);
        if(role1.getStatus() == 1){
            role.setStatus(2);
        }else {
            role.setStatus(1);
        }
        return roleMapper.updateByExampleSelective(role,example);
    }

    @Override
    public int addRole(JSONObject body) {
        String roleName=body.getString("roleName");
        String detail=body.getString("detail");
        JSONArray authorities=body.getJSONArray("authorities");

        Role role=new Role();
        role.setStatus(1);
        role.setName(roleName);
        role.setDetail(detail);
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insert(role);
        RoleExample roleExample=new RoleExample();
        roleExample.createCriteria().andNameEqualTo(roleName);
        List<Role> list=roleMapper.selectByExample(roleExample);
        int roleId=list.get(0).getId();

        for(int i=0;i<authorities.size();i++){
            RoleAuthentication roleAuthentication=new RoleAuthentication();
            roleAuthentication.setRoleId(roleId);
            roleAuthentication.setAuthenticationId(authorities.getInt(i));
            roleAuthenticationMapper.insert(roleAuthentication);
        }
        return 0;
    }

    @Override
    public List<Authentication> listRoleAuthentication(int roleId) {
        List<Authentication> list=new ArrayList();
        RoleAuthenticationExample roleAuthenticationExample=new RoleAuthenticationExample();
        roleAuthenticationExample.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleAuthentication> list1=roleAuthenticationMapper.selectByExample(roleAuthenticationExample);
        Iterator<RoleAuthentication> itr=list1.iterator();
        while (itr.hasNext()){
            AuthenticationKey authenticationKey=new AuthenticationKey();
            authenticationKey.setId(itr.next().getAuthenticationId());
            list.add(authenticationMapper.selectByPrimaryKey(authenticationKey));
        }
        return list;
    }
    @Override
    public int removeRole(int roleId) {
        RoleKey roleKey=new RoleKey();
        roleKey.setId(roleId);
        return roleMapper.deleteByPrimaryKey(roleKey);
    }
}
